<?php

// 主要是用来文件上传和图片批量上传
class Default_UploadController extends Cff_Controller_Action
{
	public function preDispatch()
	{
		$this->domain = 'http://' . $_SERVER['SERVER_NAME'];
	}
	
	public function indexAction()
	{
		$type = $this->getRequest()->getParam("type",'pic');
		if(!in_array($type,array("pic","file","simple"))){
			$type = "pic";
		}
		echo $this->view->render($type."upload.phtml"); // 视图文件为完整网页 不使用layout
	}
	
	// 文件上传
	public function uploadAction()
	{
		$upload = new Zend_File_Transfer_Adapter_Http();
		// 设置上传之后文件的目录
		$path = Default_Service_Upload::getdir(true);
		$upload->setDestination($path['abPath']);
		
		$upload->addValidator('Count', false, array(
				'min' => 1,
				'max' => 5
		)); 
		
		print_r ( $upload->getFileInfo () );//exit;
		
		$fileInfo = $upload->getFileInfo();
	
		$files = array();
		$errors = array();
		foreach ( $fileInfo as $file => $info )
		{
				
			if ($upload->isValid($file))
			{
	
				$fileName = time() . mb_convert_encoding(md5($info['name']).".".end(explode('.', $info['name'])), 'gbk', 'utf-8');
				$upload->addFilter('Rename', array(
						'target' => $fileName,	
						'overwrite' => true
				), $file);
		
				$upload->receive($file);				
				$files[] = $info['name'];
			} else
			{
				// 对不能上传的文件的错误信息进行处理
				// print_r ( $upload->getMessages () ); // 得到错误信息
				$err = array();
				foreach($upload->getMessages() as $k=>$v){
					$err[]=$v;
				}
				$errors[$info['name']] = $err;
			}
		}
		$json = array(
				"status" => 1,
				"data" => $files,
				"errors" => $errors
		);
		echo Zend_Json::encode($json);	
	}
	
	// 图片上传
	public function picUploadAction()
	{
		$upload = new Zend_File_Transfer_Adapter_Http();
		// 设置上传之后文件的目录
		$path = Default_Service_Upload::getdir(true);
		$upload->setDestination($path['abPath']);
		
		$upload->addValidator('Extension', false, 'gif,jpeg,png,jpg')->		// 设置上传文件的后缀名
		addValidator('Count', false, array(
				'min' => 1,
				'max' => 5
		))->		// 设置上传文件的个数
		addValidator('ImageSize', false, array(
				'minwidth' => 0, // 设置上传图片的大小
				'maxwidth' => 1300,
				'minhight' => 0,
				'maxhight' => 1300
		))->addValidator('FilesSize', false, array(
				'min' => '10KB',
				'max' => '1024KB'
		)); // 设置上传文件的大小
		     
		// 添加过滤器来修改上传文件的名称
		     // $upload->addFilter('Rename',array('target'=>'images/123.gif','overwrite'=>true));
		     
		$fileInfo = $upload->getFileInfo();
		
		$files = array();
		$errors = array();
		foreach ( $fileInfo as $file => $info )
		{
			
			if ($upload->isValid($file))
			{
				
				$fileName = time() . mb_convert_encoding(md5($info['name']).".".end(explode('.', $info['name'])), 'gbk', 'utf-8');
				$upload->addFilter('Rename', array(
						'target' => $fileName,
						
						'overwrite' => true
				), $file);
				
				// //保存文件
				// file_put_contents(
				// APPLICATION_PATH . "/../data/upload/" . $fileName,
				// file_get_contents ( $info ['tmp_name'] ));
				
				//保存文件
				$upload->receive($file);
				//缩略图
				$thumbArr = Default_Service_Upload::mkThumb($path['abPath'].$fileName);
// 				print_r($thumbArr);
				$files[] = preg_replace('/\//', '-', $path['path']).$fileName.$thumbArr['thumb'];
			} else
			{
				// 对不能上传的文件的错误信息进行处理
				// print_r ( $upload->getMessages () ); // 得到错误信息				
				$err = array();
				foreach($upload->getMessages() as $k=>$v){
					$err[]=$v;
				}
				$errors[$info['name']] = $err;
			}
		}
		$json = array(
				"status" => 1,
				"data" => $files,
				"errors" => $errors
		);
		echo Zend_Json::encode($json);
	
	}
	
	// 图片查看
	public function viewAction()
	{
		$fileName = $this->getRequest()->getParam("fileName");		
		$fileName = preg_replace('/-/', '/', $fileName);
		$filePath = APPLICATION_PATH . "/../data/upload/" . $fileName;
// 		print_r($filePath);exit;
		if (! file_exists($filePath))
		{
			$filePath = APPLICATION_PATH . "/../public/images/" . "noimg.jpg";
		}
		header("content-type:image/jpeg");
		echo file_get_contents($filePath);
	
	}

}